<template>
  <div>
    <kms-list-head
      ref="listHead"
      :is-search="false"
      :search.sync="search"
      :kms-type="kmsType"
      title="课程管理"
      main-title="增加课程"
      @on-handle="listHeadHandle"
      @on-search="financeSearch"
      @on-export="exportInfos"/>
    <div
      xs-empty-icon="nopermission"
      xs-empty-text="暂无权限"
      class="finance-container">
      <el-table
        v-loading="loading"
        id="kms-table"
        :data="list"
        :height="tableHeight"
        :cell-style="cellStyle"
        class="n-table--border"
        stripe
        border
        highlight-current-row
        style="width: 100%"
        @row-click="handleRowClick"
        @sort-change="sortChange"
        @header-dragend="handleHeaderDragend"
        @selection-change="handleSelectionChange">
        <el-table-column
          v-for="(item, index) in fieldList"
          :key="index"
          :fixed="index==0"
          :prop="item.prop"
          :label="item.label"
          :formatter="fieldFormatter"
          show-overflow-tooltip>
          <template
            slot-scope="scope">
            <div :title="scope.row[item.prop]" class="table-head-name">{{ scope.row[item.prop] }}</div>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          min-width="120">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.prevent="editCrate(scope.row)">
              编辑
            </el-button>
            <el-button
              type="text"
              size="small"
              @click.native.prevent="deleteRow(scope.row)">
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="p-contianer">
        <el-pagination
          :current-page="currentPage"
          :page-sizes="pageSizes"
          :page-size.sync="pageSize"
          :total="total"
          class="p-bar"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"/>
      </div>
    </div>
  </div>
</template>

<script>
import table from '../mixins/table'
import {
  // eslint-disable-next-line no-unused-vars
  getCourseList, deleteCourse
} from '../../../api/kmsmanagement/course'
import kmsTypeModel from '@/views/kmsmanagement/model/kmsTypeModels'
export default {
  /** 客户管理 的 产品列表 */
  name: 'SourceIndex',
  mixins: [table],
  data() {
    return {
      kmsType: 'course',
      five_start_dis: true,
      fileValue: ''
    }
  },
  computed: {},
  mounted() {
    /** 控制table的高度 */
    window.onresize = () => {
      this.updateTableHeight()
    }
    if (this.kms[this.kmsType].index) {
      this.loading = true
      this.getFieldList()
      this.getList()
    }
  },
  methods: {
    /** 通过回调控制style */
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (column.property === 'name') {
        return { color: '#3E84E9', cursor: 'pointer' }
      } else {
        return ''
      }
    },
    deleteRow(val) {
      this.$confirm('确定要删除这些数据吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.confirmHandle(val.courseId)
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消操作'
          })
        })
    },
    confirmHandle(id) {
      var param = {}
      param = {
        courseIds: id
      }
      deleteCourse(param)
        .then(res => {
          this.$message({
            type: 'success',
            message: '删除成功'
          })
          this.getList()
        })
        .catch(() => {
        })
    },
    editCrate(val) {
      const data = {
        type: 'update',
        id: val.courseId,
        data: {}
      }
      this.$refs.listHead.createClick(data)
    },
    getList() {
      this.loading = true
      var params = {
        page: this.currentPage,
        limit: this.pageSize,
        search: this.search,
        type: kmsTypeModel[this.kmsType]
      }
      if (this.sortData.order) {
        params.sortField = this.sortData.prop
        params.order = this.sortData.order == 'ascending' ? 2 : 1
      }

      if (this.sceneId) {
        params.sceneId = this.sceneId
      }
      if (this.filterObj && Object.keys(this.filterObj).length > 0) {
        params.data = this.filterObj
      }
      getCourseList(params)
        .then(res => {
          this.list = res.data.list
          this.total = res.data.totalRow
          this.loading = false
        })
        .catch(() => {
          this.loading = false
        })
    }
  }
}
</script>

<style lang="scss" scoped>
  @import '../../financemanagement/styles/table.scss';
</style>

